package com.ibiaoke.boss.controller;

import com.ibiaoke.boss.service.LoginService;
import com.ibiaoke.common.base.BaseController;
import com.ibiaoke.common.consts.CommonConsts;
import com.ibiaoke.common.consts.WeChatConsts;
import com.ibiaoke.common.response.Res;
import com.ibiaoke.common.utils.AssertUtils;
import com.ibiaoke.customer.entity.Customer;
import com.ibiaoke.customer.entity.ThirdPartLogin;
import com.ibiaoke.customer.service.CustomerService;
import com.ibiaoke.customer.service.ThirdPartLoginService;
import com.ibiaoke.marketing.service.CouponReceiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by huhaichao on 2017/6/21.
 */
@Controller
public class WxLoginController extends BaseController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private CouponReceiveService couponReceiveService;

    @Autowired
    private ThirdPartLoginService thirdPartLoginService;

    /**
     * 登陆
     *
     * @param userName
     * @param password
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public Res login(String userName, String password) {
        Customer customer = loginService.login(userName, password);
        getSession().setAttribute(CommonConsts.LOGIN_SESSION_NAME, customer);
        return Res.create()
                .addResponse(CommonConsts.TOKEN, customer.getToken())
                .addResponse("hasPassword", customer.getCustomerPassword() == null ? 0 : 1)
                .addResponse("customerId",customer.getCustomerId())
                .addResponse("mobile",customer.getCustomerMobile())
                .addResponse("name",customer.getCustomerNickname())   ;
    }

    /**
     * APP快捷登录
     * @param mobile
     * @param code
     * @return
     */
    @RequestMapping("/loginByMobileVerify")
    @ResponseBody
    public Res loginByMobileVerify(String mobile, String code) {
        Customer customer = loginService.loginByMobileVerify(mobile, code);
        getSession().setAttribute(CommonConsts.LOGIN_SESSION_NAME, customer);
        return Res.create()
                .addResponse(CommonConsts.TOKEN,customer.getToken())
                .addResponse("hasPassword",customer.getCustomerPassword()==null?0:1)
                .addResponse("customerId",customer.getCustomerId())
                .addResponse("mobile",customer.getCustomerMobile())
                .addResponse("name",customer.getCustomerNickname());
    }

    /**
     * 退出
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/logout")
    public Res logout() {
        getSession().invalidate();
        return Res.create();
    }

    /**
     * 判断是否登陆
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/isLogin")
    public Res isLogin() {
        Customer customer = getLoginUser();
        return Res.create().addResponse("isLogin", customer == null ? CommonConsts.NO : CommonConsts.YES);
    }



    @ResponseBody
    @RequestMapping("/getNickNameandHeadImage")
    public Res getNickNameandHeadImage() {
        Customer customer = getLoginUser();
        String openId = (String) getSession().getAttribute(WeChatConsts.OPEN_ID);
        AssertUtils.notNull(customer, "请登录");
        AssertUtils.notBlank(openId, "请获取openId");
        ThirdPartLogin thirdPartLogin = thirdPartLoginService.getByUid(openId);
        return Res.create().addResponse("thirdPartinfomation", thirdPartLogin);
    }

    /**
     * 设置登录密码
     * @param password
     * @return
     */
    @ResponseBody
    @RequestMapping("/setLoginPassword")
    public Res setLoginPassword(String password,String token) {
        Customer customer = getLoginUser();
        AssertUtils.notNull(customer,"请先登录");
        customerService.editLoginPassword(customer.getCustomerMobile(),password);
        return Res.create();
    }

    /**
     * 修改登录密码
     * @param password
     * @param newPassword
     * @param token
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateLoginPassword")
    public Res updateLoginPassword(String password,String newPassword,String token) {
        Customer customer = getLoginUser();
        AssertUtils.notNull(customer,"请先登录");
        customerService.updateLoginPassword(customer.getCustomerMobile(),password,newPassword);
        return Res.create();
    }

    /**
     * 忘记密码
     * @param mobile
     * @param code
     * @param newPassword
     * @return
     */
    @ResponseBody
    @RequestMapping("/forgetLoginPassword")
    public Res forgetLoginPassword(String mobile,String code,String newPassword){
        customerService.forgetLoginPassword(code, newPassword,mobile);
        return Res.create();
    }

    @ResponseBody
    @RequestMapping("/appLogout")
    public Res appLogout() {
        getSession().invalidate();
        return Res.create();
    }

}
